package com.hz.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hz.web.entity.HomePageEntity;
import com.hz.web.entity.UserEntity;
import com.hz.web.service.IhomepageService;
import com.hz.web.util.Constant;

@Controller
@RequestMapping("homepage")
public class HomepageController {

	@Resource 
	private IhomepageService homepageService;
	
	@ResponseBody
	@RequestMapping("queryHomePage")
	public List<HomePageEntity> queryHomePage(String homeType){
		List<HomePageEntity> list = homepageService.queryHomePage(homeType);
		return list;
	}

	@ResponseBody
	@RequestMapping("addHomePage")
	public Map<String,Object> addHomePage(HttpSession session,HomePageEntity homepage){
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			UserEntity entity =null;
			if(session.getAttribute("userinfo")!=null){
				entity = (UserEntity) session.getAttribute("userinfo");
				homepage.setChangeUser(String.valueOf(entity.getId()));
				homepageService.addHomePage(homepage);
				return Constant.MSG.SAVE_OK();
			}else{
				
				map.put("title","失败");
				map.put("content","当前用户未登录");
				map.put("success","error");
				return map;
			}	
		} catch (Exception e) {
			return Constant.MSG.SAVE_ERROR();
		}
	}
	
	@ResponseBody
	@RequestMapping("updateHomepage")
	public Map<String,Object> updateHomepage(HttpSession session,HomePageEntity homepage){
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			UserEntity entity =null;
			if(session.getAttribute("userinfo")!=null){
				entity = (UserEntity) session.getAttribute("userinfo");
				homepage.setChangeUser(String.valueOf(entity.getId()));
				homepageService.updateHomePage(homepage);
				return Constant.MSG.SAVE_OK();
			}else{
				map.put("title","失败");
				map.put("content","当前用户未登录");
				map.put("success","error");
				return map;
			}
		} catch (Exception e) {
			return Constant.MSG.UPD_ERROR();
		}
	}
	
	@ResponseBody
	@RequestMapping("delHomePage")
	public Map<String,Object> delHomePage(String homeId){
		try {
			homepageService.delHomePage(homeId);
			return Constant.MSG.DEL_OK();
		} catch (Exception e) {
			// TODO: handle exception
			return Constant.MSG.DEL_ERROR();
		}
	}
}
